package com.itlong.cloud.controller.system;

import com.itlong.cloud.POJO.DTO.property.PropertyFunctionGetPageDTO;
import com.itlong.cloud.POJO.PO.PropertyFunctionPO;
import com.itlong.cloud.POJO.PO.PropertyRolePO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyFunctionVO;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.property.IPropertyFunctionServiceClient;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营-系统-物业菜单管理控制器
 * </desc>
 *
 * @createDate 2017/10/09
 */
@RestController
@RequestMapping("/system/propertyFunction")
public class PropertyFunctionController {
    private static final Logger LOG = LoggerFactory.getLogger(OperateFunctionController.class);
    @Autowired
    IPropertyFunctionServiceClient iPropertyFunctionServiceClient;

    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param propertyFunctionGetPageDTO 查询参数DTO
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @RequestMapping(path = "getPage",method = RequestMethod.POST)
    @ResponseBody
    public Object getPropertyFunctionPage(PropertyFunctionGetPageDTO propertyFunctionGetPageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyFunctionGetPageDTO= HttpProtocolUtil.parseRequestParamToDTO(PropertyFunctionGetPageDTO.class,request);
            Page<PropertyFunctionPO> page=iPropertyFunctionServiceClient.getPage(propertyFunctionGetPageDTO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【运营_系统_物业资源菜单getPage】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      添加资源
     * </desc>
     *
     * @param propertyFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_PROPERTY_LOG_SAVE)
    @RequestMapping(path = "save",method = RequestMethod.POST)
    @ResponseBody
    public Object savePropertyFunction(PropertyFunctionPO propertyFunctionPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(PropertyFunctionPO.class,request);
            iPropertyFunctionServiceClient.save(propertyFunctionPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_系统_物业资源菜单save】添加资源失败",e);
        }
        return messageVO;

    }

    /**
     * <desc>
     *      修改资源
     * </desc>
     *
     * @param propertyFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_PROPERTY_LOG_UPDATE)
    @RequestMapping(path = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object updatePropertyFunction(PropertyFunctionPO propertyFunctionPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(PropertyFunctionPO.class,request);
            iPropertyFunctionServiceClient.update(propertyFunctionPO);
            messageVO=new MessageVO(propertyFunctionPO.getFunctionId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-_系统_物业资源菜单update】更新资源失败", e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      查询单条资源
     * </desc>
     *
     * @param propertyFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @RequestMapping(path = "getOne",method = RequestMethod.POST)
    @ResponseBody
    public Object getPropertyFunction(PropertyFunctionPO propertyFunctionPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(PropertyFunctionPO.class,request);
            propertyFunctionPO=iPropertyFunctionServiceClient.getOne(propertyFunctionPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),propertyFunctionPO);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_物业资源菜单getOne】获取单条资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      删除资源
     * </desc>
     *
     * @param functionIds 资源id数组字符串 ，号分隔
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_PROPERTY_LOG_DELETE)
    @RequestMapping(path = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Object deletePropertyFunction(String functionIds){
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            functionIds=request.getAttribute("functionIds")==null?null:request.getAttribute("functionIds").toString();
            iPropertyFunctionServiceClient.delete(functionIds);
            messageVO=new MessageVO(functionIds,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch(Exception e){
            throw new DataAccessException("【运营_系统_物业资源菜单delete】删除资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      查询下级节点
     * </desc>
     *
     * @param functionId 资源id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @RequestMapping(path = "getChilds",method = RequestMethod.POST)
    @ResponseBody
    public Object getPropertyFunctionChilds(String functionId){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            functionId=request.getAttribute("functionId")==null?null:request.getAttribute("functionId").toString();
            List<PropertyFunctionVO> list=iPropertyFunctionServiceClient.getChilds(functionId);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_物业资源菜单getChilds】查询下级节点失败",e);
        }
        return messageVO;
    }


    /**
     * <desc>
     *      启用或禁用
     * </desc>
     *
     * @param propertyFunctionPO 资源实体
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @LoggerInfo(operateDesc = LogDescConstant.FUNCTION_PROPERTY_LOG_BANORUSE)
    @RequestMapping(path = "banOrUse",method = RequestMethod.POST)
    @ResponseBody
    public Object banOrUsePropertyFunction(PropertyFunctionPO propertyFunctionPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyFunctionPO= HttpProtocolUtil.parseRequestParamToDTO(PropertyFunctionPO.class,request);
            iPropertyFunctionServiceClient.update(propertyFunctionPO);
            messageVO=new MessageVO(propertyFunctionPO.getFunctionId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_物业资源菜单banOrUse】启用或禁用失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取分配菜单所有节点
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @RequestMapping(path = "getAllotFunction",method = RequestMethod.POST)
    @ResponseBody
    public Object getAllotPropertyFunction(PropertyRolePO propertyRolePO,String projectId){
        MessageVO messageVO=null;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyRolePO= HttpProtocolUtil.parseRequestParamToDTO(PropertyRolePO.class,request);
            projectId=request.getAttribute("projectId")==null?null:request.getAttribute("projectId").toString();
            String roleId = StringUtils.isBlank(propertyRolePO.getRoleId()) ? "" : propertyRolePO.getRoleId();
            String projId = StringUtils.isBlank(projectId) ? "" : projectId;

            List<PropertyFunctionVO> list=iPropertyFunctionServiceClient.getAllotFunction(roleId, projId);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_物业资源菜单getAllotFunction】获取分配菜单所有节点失败");
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取当前节点的父级节点以及同级别所有父级节点
     * </desc>
     *
     * @param parent 资源id
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
    @RequestMapping(path = "getParents",method = RequestMethod.POST)
    @ResponseBody
    public Object getParentLevelFunctions(String parent){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            parent=request.getAttribute("parent")==null?null:request.getAttribute("parent").toString();
            List<PropertyFunctionPO> list=iPropertyFunctionServiceClient.getParentLevelFunctions(parent);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_物业资源菜单getParentLevelFunctions】获取当前节点的父级节点以及同级别所有父级节点失败");
        }
        return messageVO;
    }

    /**
     * <desc>
     *      更新1.6用户权限
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/09
     */
//    @RequestMapping(path = "updateV16RoleFunctions")
//    @ResponseBody
//    public Object updateV16RoleFunctions(){
//        MessageVO messageVO;
//        try{
//            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyFunctionServiceClient.handleV16FunctionUpdate());
//        }catch (Exception e){
//            throw new DataAccessException("【运营_系统_物业资源菜单getParentLevelFunctions】更新V1.6角色权限");
//        }
//        return messageVO;
//    }
}
